package com.project.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lyl
 * description:
 */
@Controller
@RequestMapping("scope")
public class ScopeController {
    /**
     * 绑定共享数据
     *
     * @param request
     * @return
     */
    @RequestMapping("bind")
    public String bindData(HttpServletRequest request) {
        //在请求范围内共享数据
        request.setAttribute("request", "responses数据");
        //在用户会话中共享数据
        request.getSession().setAttribute("session", "session数据");
        //在全局范围内共享数据
        request.getServletContext().setAttribute("context", "context数据");
        return "/scope/get";
    }

    @ResponseBody
    @RequestMapping("get")
    public String getData(HttpServletRequest request) {
        String str = "";
        str += "在request绑定共享数据" + request.getAttribute("request") + "<br>";
        str += "在HttpSession中绑定共享数据" + request.getSession().getAttribute("session") + "<br>";
        str += "在context中绑定共享数据" + request.getServletContext().getAttribute("context") + "<br>";

        request.getSession().invalidate();

        return str;
    }
}
